package com.business_clean.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.business_clean.app.App
import com.business_clean.app.config.Constant
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.address.AddressEntity
import com.business_clean.data.mode.address.AddressListEntity
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class AddressViewModel : BaseViewModel() {

    var addressManager = MutableLiveData<AddressEntity>()

    var addressDetailManager = MutableLiveData<AddressListEntity>()

    var saveAddress = MutableLiveData<Any>()

    var delsStatus = MutableLiveData<Any>()

    /**
     * 获取全部地址
     */
    fun requestAddressAll(page: Int) {
        rxHttpRequest {
            onRequest = {
                addressManager.value = RxHttp.get(NetUrl.GET_ADDRESS_ALL)
                    .add("page", page)
                    .add("size", Constant.PAGE_SIZE)
                    .add("project_uuid", App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<AddressEntity>().await()
            }
        }
    }


    /**
     * 获取地址详情
     */
    fun requestAddressDetail(uuid: String) {
        rxHttpRequest {
            onRequest = {
                addressDetailManager.value = RxHttp.get(NetUrl.GET_ADDRESS_DETIALI)
                    .add("uuid", uuid)
                    .add("project_uuid", App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<AddressListEntity>().await()
            }
        }
    }

    /**
     * 新建地址
     */
    fun requestSaveAddress(hashMap: HashMap<String, String>) {
        rxHttpRequest {
            onRequest = {
                saveAddress.value = RxHttp.get(NetUrl.SAVE_ADDRESS)
                    .addAll(hashMap)
                    .toResponse<Any>().await()
            }
        }
    }

    /**
     * 删除地址
     */
    fun delAddress(uuid: String) {
        rxHttpRequest {
            onRequest = {
                delsStatus.value = RxHttp.get(NetUrl.DEL_ADDRESS)
                    .add("uuid", uuid)
                    .add("project_uuid", App.appViewModelInstance.userInfo.value?.project?.uuid)
                    .toResponse<Any>().await()
            }
        }
    }

}